﻿using System;
using System.Linq;
using System.Text;
using RAP.Framework.Libary.DomTemplateBuilder.Element.DictionaryActions.Add;
using RAP.Framework.Libary.Utils;
using RAP.Framework.Libary.DomTemplateBuilder.Interface;

namespace RAP.Framework.Libary.DomTemplateBuilder.Handler.DictionaryActions.Add
{
	/// <summary>
	/// 添加一个值对象到字典项的Handler
	/// </summary>
	public class AddValueActionHandler : DictionaryAddActionHandler
    {
        public new AddValueAction Element
        {
            get
            {
                return base.Element as AddValueAction;
            }
            set
            {
                base.Element = value;
            }
        }

        public override void DoRender ( StringBuilder writer, object rootParamObject, object parentParamObject, ITemplateRenderContext context )
        {
			var sbValue = new StringBuilder ( );
            base.DoRenderChildNodes ( sbValue, rootParamObject, parentParamObject, context );
			object value = sbValue.ToString ( ).Trim ( );
			var dict = context.ToDictionary( DictionaryActionHandler.DICTIONARY_OBJECT );
			var type = this.Element.ValueType;
            if ( type != TypeCode.Object )
            {
                value = value.ConvertValue ( type );
            }
            else
            {
                value = value.ToString ( ).Eval ( );
            }
			var property = this.Element.Parent.Handler.GetPropertyName ( rootParamObject, parentParamObject, context );
			dict[ property ] = value;
        }
    }
}
